package cn.stylefeng.guns.modular.classicCase.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.classicCase.entity.ClassicCase;
import cn.stylefeng.guns.modular.classicCase.enums.ClassicCaseExceptionEnum;
import cn.stylefeng.guns.modular.classicCase.mapper.ClassicCaseMapper;
import cn.stylefeng.guns.modular.classicCase.pojo.request.ClassicCaseRequest;
import cn.stylefeng.guns.modular.classicCase.service.ClassicCaseService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 经典案例业务实现层
 *
 * @author liuxue
 * @date 2022/01/10 15:10
 */
@Service
public class ClassicCaseServiceImpl extends ServiceImpl<ClassicCaseMapper, ClassicCase> implements ClassicCaseService {

	@Override
    public void add(ClassicCaseRequest classicCaseRequest) {
        ClassicCase classicCase = new ClassicCase();
        BeanUtil.copyProperties(classicCaseRequest, classicCase);
        this.save(classicCase);
    }

    @Override
    public void del(ClassicCaseRequest classicCaseRequest) {
        ClassicCase classicCase = this.queryClassicCase(classicCaseRequest);
        this.removeById(classicCase.getClassicCaseId());
    }

    @Override
    public void edit(ClassicCaseRequest classicCaseRequest) {
        ClassicCase classicCase = this.queryClassicCase(classicCaseRequest);
        BeanUtil.copyProperties(classicCaseRequest, classicCase);
        this.updateById(classicCase);
    }

    @Override
    public ClassicCase detail(ClassicCaseRequest classicCaseRequest) {
        return this.queryClassicCase(classicCaseRequest);
    }

    @Override
    public PageResult<ClassicCase> findPage(ClassicCaseRequest classicCaseRequest) {
        LambdaQueryWrapper<ClassicCase> wrapper = createWrapper(classicCaseRequest);
        wrapper.orderByAsc(ClassicCase::getSort);
        Page<ClassicCase> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<ClassicCase> findList(ClassicCaseRequest classicCaseRequest) {
        LambdaQueryWrapper<ClassicCase> wrapper = this.createWrapper(classicCaseRequest);
        wrapper.orderByAsc(ClassicCase::getSort);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author liuxue
     * @date 2022/01/10 15:10
     */
    private ClassicCase queryClassicCase(ClassicCaseRequest classicCaseRequest) {
        ClassicCase classicCase = this.getById(classicCaseRequest.getClassicCaseId());
        if (ObjectUtil.isEmpty(classicCase)) {
            throw new ServiceException(ClassicCaseExceptionEnum.CLASSICCASE_NOT_EXISTED);
        }
        return classicCase;
    }

    /**
     * 创建查询wrapper
     *
     * @author liuxue
     * @date 2022/01/10 15:10
     */
    private LambdaQueryWrapper<ClassicCase> createWrapper(ClassicCaseRequest classicCaseRequest) {
        LambdaQueryWrapper<ClassicCase> queryWrapper = new LambdaQueryWrapper<>();

        Integer classicCaseId = classicCaseRequest.getClassicCaseId();
        Long fileId = classicCaseRequest.getFileId();
        String title = classicCaseRequest.getTitle();
        String type = classicCaseRequest.getType();
        String details = classicCaseRequest.getDetails();
        String Introduction = classicCaseRequest.getIntroduction();
        Integer sort = classicCaseRequest.getSort();
        Integer status = classicCaseRequest.getStatus();

        queryWrapper.eq(ObjectUtil.isNotNull(classicCaseId), ClassicCase::getClassicCaseId, classicCaseId);
        queryWrapper.eq(ObjectUtil.isNotNull(fileId), ClassicCase::getFileId, fileId);
        queryWrapper.like(ObjectUtil.isNotEmpty(type), ClassicCase::getType, type);
        queryWrapper.like(ObjectUtil.isNotEmpty(title), ClassicCase::getTitle, title);
        queryWrapper.like(ObjectUtil.isNotEmpty(details), ClassicCase::getDetails, details);
        queryWrapper.like(ObjectUtil.isNotEmpty(Introduction), ClassicCase::getIntroduction, Introduction);
        queryWrapper.eq(ObjectUtil.isNotNull(sort), ClassicCase::getSort, sort);
        queryWrapper.eq(ObjectUtil.isNotNull(status), ClassicCase::getStatus, status);

        return queryWrapper;
    }

}